#include <iostream>
#include <cstdlib>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

using namespace std;

int main()
{
    int key = 1234;
    int shmid = shmget(key, 4096, IPC_CREAT);
    // 挂接到当前进程
    cout << "开始挂接" << endl;
    char *s = (char *)shmat(shmid, nullptr, 0);
    cout << "开始从共享内存中读取数据" << endl;
    cout << s << endl;
    cout << "即将解除关联" << endl;
    sleep(1);
    cout << "解除关联" << endl;
    shmdt(s);
    sleep(1);
    cout << "进程退出" << endl;
    sleep(2);
    return 0;
}